package com.zzvcom.office.services;

import cn.hutool.core.io.FileUtil;
import com.zzvcom.office.entities.OfficeFile;
import com.zzvcom.office.repositories.OfficeFileRepository;
import com.zzvcom.office.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;

/**
 *
 */
@Slf4j
@Service
public class OfficeFileServices {
    @Autowired
    private OfficeFileRepository officeFileRepository;

    /**
     * 获取文件信息
     * @param fileId 文件编码
     * @return
     */
    @Cacheable(value = "officefile", key = "#fileId", unless = "#result == null", cacheManager = "officeFileCacheManager")
    public OfficeFile getById(String fileId) {
        log.info("getById fileId={}", fileId);
        Optional<OfficeFile> byId = officeFileRepository.findById(fileId);
        return byId.isPresent() ? byId.get() : null;

    }
    @CacheEvict(value = "officefile",key = "#officeFile.id",cacheManager = "officeFileCacheManager")
    public void save(OfficeFile officeFile) {
        officeFileRepository.save(officeFile);
    }

    public List<OfficeFile> findAll() {
        return officeFileRepository.findAll();
    }
}
